--[[
 -- *名称:Card
 -- *描述:牌类
 -- *版权:Copyright © 2016-2018 深圳市三只小熊科技有限公司 版权所有
 -- *作者:Administrator
 -- *创建日期:2017-11-07
 -- *修改日期:2017-11-07
 -- @return [description]
 ]]

local Card = pokercore.class();


--设置元表
local mt = {};

---
 -- [__eq 相等]
 -- @DateTime 2017-11-07
 -- @param    c1 [description]
 -- @param    c2 [description]
 -- @return    [description]
 --
mt.__eq = function (c1, c2) 
--print("dsadsadsadasd")
   if not c2 or not c1 then
      return false ;
   elseif c1 == c2 then
      return true ;
   end 
   return (c1.cardShape  ==  c2.cardShape) and (c1.number  ==  c2.number) ;
end

---
 -- [__lt 小于]
 -- @DateTime 2017-11-07
 -- @param    c1 [description]
 -- @param    c2 [description]
 -- @return    [description]
 --
mt.__lt = function (c1, c2) 
    if not c1 then
       return true; 
    elseif not c2 then
       return false ; 
    else
      return (c1.value - c2.value) < 0 and true or false ;
    end
end

---
 -- [__le 不大于]
 -- @DateTime 2017-11-07
 -- @param    c1 [description]
 -- @param    c2 [description]
 -- @return    [description]
 --
mt.__le = function (c1, c2) 
    if not c1 then
       return true; 
    elseif not c2 then
       return false ; 
    else
      return (c1.value - c2.value) <= 0 and true or false ;
    end
end


mt.__tostring = function (t)
   return "" ..(t.cardShape:getSimpleName() or "nil") .. " " ..",number->".. (t.number or "nil") .. ",value -> "..(t.value or "nil")
   .. ",isLaiZi ->"..(t.isLaizi == nil and "nil" or tostring(t.isLaizi));
end


---
 -- [ctor 构造方法]
 -- @DateTime 2017-11-07
 -- @param    cardShape 花色
 -- @param    number 点数
 -- @param    value 权重
 -- @return    [description]
 --
function Card:ctor(cardShape,number,value)
   self.cardShape  =  cardShape ;
   self.number = number ;
   self.value = value ;

   local meta = getmetatable(self) or {};
   table.merge(meta,mt)
   setmetatable(self,meta)
end



function Card:dtor()

end

---
 -- [revalue 重置权重]
 -- @DateTime 2017-11-07
 -- @return   [description]
 --
function Card:revalue()
	if self.cardShape ==  CardShape.RED_JOKER then
        self.number = PokerGameConstant.RED_JOKER_SHAPE_NUM;
		self.value = PokerGameConstant.RED_JOKER_SHAPE_VALUE;
	elseif self.cardShape == CardShape.BLACK_JOKER then
		self.number = PokerGameConstant.BLACK_JOKER_SHAPE_NUM;
  		self.value = PokerGameConstant.BLACK_JOKER_SHAPE_VALUE;
	else 
		if self.number == 1  then
			self.value = PokerGameConstant.NUMBER_1_VALUE;
		elseif self.number == 2 then
			self.value = PokerGameConstant.NUMBER_2_VALUE;
		else 
			self.value = self.number;
		end
    end
	return self;

end

---
 -- [clone 克隆一张牌]
 -- @DateTime 2017-11-07
 -- @return   [description]
 --
function Card:clone()
   return pokercore.clone(self) ;
end


---
 -- [compareTo 和其他牌比较大小]
 -- @DateTime 2017-11-07
 -- @param    otherCard [description]
 -- @return    [description]
 --
function Card:compareTo(otherCard)
   if not otherCard then
      return 1 ;
   else 
   	  return self.value - otherCard.value ;
   end
end


---
 -- [compareWithShapeTo 
 --* 带花色的比较，用于排序 <br>
 --* 黑红梅方 value 小的在前面]
 -- @DateTime 2017-11-07
 -- @param    otherCard [description]
 -- @return    [description]
 --
function Card:compareWithShapeTo(otherCard)
  if not otherCard then
			return 1;
  else
		local weight_c = self.value - otherCard.value;
		if weight_c == 0 then
			if not otherCard.cardShape then
				return 1;
			else
				return self.cardShape:getShape() - otherCard.cardShape:getShape();
			end
		else 
			return weight_c;	
	    end
	end
end


function Card:getValue()
  return self.value ;
end

function Card:setValue(value)
  assert(value,"card value 不能赋值为nil")
  self.value = value ;
  ----print("Card:setValue",value);
end

function Card:getCardShape()
  return self.cardShape ;
end

function Card:getNumber(value)
  return self.number ;
end

function Card:setNumber(number)
  self.number = number ;
end

--@Override
function Card:equals(obj) 
  if (obj == null) then
    return false;
  end
  if (self.cardShape ~= obj.cardShape) then
    return false;
  end
  if (self.number ~= obj.number) then
    return false;
  end
  return true;
end




return Card ;
